package ds.tree;

public class MaxWidthOfTree {
	
	public int height(TreeNode node){
		if(node == null){
			return 0;
		}
		return 1+Math.max(height(node.left), height(node.right));
	}
	public int getMaxWidth(TreeNode node){
		int maxWidth = 0;
		if(node != null ){
			int height = height(node);			
			for (int i = 1; i <= height; i++) {
				int width = getWidth(node, i);
				if(width > maxWidth){
					maxWidth = width;
				}
			}
		}
		return maxWidth;
	}
	
	public int getWidth(TreeNode node, int level){
		if(node == null){
			return 0;
		}
		if(level == 1) {
			return 1;
		}
		return getWidth(node.left,level-1)+getWidth(node.right,level-1);
	}
	private static TreeNode tree(){
		TreeNode A = new TreeNode(1);
		TreeNode B = new TreeNode(2);
		TreeNode C = new TreeNode(3);
		TreeNode D = new TreeNode(4);
		TreeNode E = new TreeNode(5);
		
		A.left = B;
		A.right = C;
		B.left = D;
		B.right = E;
		
		return A;
	}
	private static class TreeNode{
		int value;
		TreeNode left = null;
		TreeNode right = null;
		
		TreeNode(int val){
			this.value = val;
		}
		
		public String toString(){
			return String.valueOf(value);
		}
	}
	public static void main(String[] args) {
		MaxWidthOfTree maxWidth = new MaxWidthOfTree();
		System.out.println("Max Width of Tree = " + maxWidth.getMaxWidth(tree()));
	}

}
